package cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestflow;

import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.mqtt.controller.admin.devicelatestflow.vo.*;
import cn.iocoder.yudao.module.mqtt.dal.dataobject.devicelatestflow.DeviceLatestFlowDO;
import cn.iocoder.yudao.module.mqtt.service.devicelatestflow.DeviceLatestFlowService;

@Tag(name = "管理后台 - 设备最新信息")
@RestController
@RequestMapping("/mqtt/device-latest")
@Validated
public class DeviceLatestFlowController {

    @Resource
    private DeviceLatestFlowService deviceLatestService;

    @PostMapping("/create")
    @Operation(summary = "创建设备最新信息")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest:create')")
    public CommonResult<Long> createDeviceLatest(@Valid @RequestBody DeviceLatestFlowSaveReqVO createReqVO) {
        return success(deviceLatestService.createDeviceLatest(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备最新信息")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest:update')")
    public CommonResult<Boolean> updateDeviceLatest(@Valid @RequestBody DeviceLatestFlowSaveReqVO updateReqVO) {
        deviceLatestService.updateDeviceLatest(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备最新信息")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest:delete')")
    public CommonResult<Boolean> deleteDeviceLatest(@RequestParam("id") Long id) {
        deviceLatestService.deleteDeviceLatest(id);
        return success(true);
    }

    @DeleteMapping("/delete-list")
    @Parameter(name = "ids", description = "编号", required = true)
    @Operation(summary = "批量删除设备最新信息")
                @PreAuthorize("@ss.hasPermission('mqtt:device-latest:delete')")
    public CommonResult<Boolean> deleteDeviceLatestList(@RequestParam("ids") List<Long> ids) {
        deviceLatestService.deleteDeviceLatestListByIds(ids);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备最新信息")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest:query')")
    public CommonResult<DeviceLatestFlowRespVO> getDeviceLatest(@RequestParam("id") Long id) {
        DeviceLatestFlowDO deviceLatest = deviceLatestService.getDeviceLatest(id);
        return success(BeanUtils.toBean(deviceLatest, DeviceLatestFlowRespVO.class));
    }

    @GetMapping("/getDeviceKey")
    @Operation(summary = "获得设备最新信息")
    @Parameter(name = "deviceKey", description = "设备KEY", required = true, example = "1024")
    public CommonResult<DeviceLatestFlowRespVO> getDeviceKey(@RequestParam("deviceKey") String deviceKey) {
        DeviceLatestFlowDO deviceLatest = deviceLatestService.getDeviceKey(deviceKey);
        return success(BeanUtils.toBean(deviceLatest, DeviceLatestFlowRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备最新信息分页")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest:query')")
    public CommonResult<PageResult<DeviceLatestFlowRespVO>> getDeviceLatestPage(@Valid DeviceLatestFlowPageReqVO pageReqVO) {
        PageResult<DeviceLatestFlowDO> pageResult = deviceLatestService.getDeviceLatestPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DeviceLatestFlowRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备最新信息 Excel")
    @PreAuthorize("@ss.hasPermission('mqtt:device-latest:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportDeviceLatestExcel(@Valid DeviceLatestFlowPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DeviceLatestFlowDO> list = deviceLatestService.getDeviceLatestPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "设备最新信息.xls", "数据", DeviceLatestFlowRespVO.class,
                        BeanUtils.toBean(list, DeviceLatestFlowRespVO.class));
    }

}